//
//  LFGSInforController.m
//  ServiceBaZhou
//
//  Created by 刘江涛 on 2017/8/23.
//  Copyright © 2017年 weierda. All rights reserved.
//

#import "LFGSInforController.h"

#import "ShareholdCell.h"

#import "ChangeInforCell.h"

#import "GSInforModel.h"

#import "GSPartnerModel.h"

#import "GSEmployeeModel.h"

#import "GSRecordModel.h"

#import "GSBranchModel.h"

#import "CFToolUtil.h"


#define UpImage [UIImage imageNamed:@"up"]

#define DownImage [UIImage imageNamed:@"icon_down"]

@interface LFGSInforController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) NSArray *localDataArray;

@property (nonatomic,strong) NSArray *webDataArray;

@property (nonatomic,strong) NSArray *headTitleArray;

@property (nonatomic,strong) NSMutableArray *GSPartnerArray;

@property (nonatomic,strong) NSMutableArray *GSEmployeeArray;

@property (nonatomic,strong) NSMutableArray *GSRecordArray;

@property (nonatomic,strong) NSMutableArray *GSBranchArray;

@property (nonatomic,strong) NSArray *headNumArray;

@property (nonatomic,strong) NSMutableArray *closeArray;

@end

@implementation LFGSInforController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _headTitleArray = @[@"登记信息",@"股东",@"主要人员",@"变更记录",@"分支结构"];
    
    [self createNavigation:_titleText];
    
    [self createTableView];
    
    [self loadData];
    
}

- (void)createTableView{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, Width, Height - 64) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [_tableView registerNib:[UINib nibWithNibName:@"ShareholdCell" bundle:nil] forCellReuseIdentifier:@"ShareholdCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"ChangeInforCell" bundle:nil] forCellReuseIdentifier:@"ChangeInforCell"];
    
    
    [self.view addSubview:_tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if ([_closeArray[section] intValue]) {
        return 0;
    }
    
    switch (section) {
        case 0://基本信息
            return _localDataArray.count;
            break;
            
        case 1://股东信息
            return _GSPartnerArray.count;
            break;
            
        case 2://高管信息
            return _GSEmployeeArray.count;
            break;
            
        case 3://变更记录
            return _GSRecordArray.count;
            break;
            
        default://分支信息
            return _GSBranchArray.count;
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GSRecordModel *model = _GSRecordArray[indexPath.row];
    
    switch (indexPath.section) {
        case 0://基本信息(动态布局  经营范围数据)
            if([CFToolUtil getLabelHeightWith:_webDataArray[indexPath.row] AndStrSize:11 AndLabelWith:Width - 80] > 30){
                return 30 + [CFToolUtil getLabelHeightWith:_webDataArray[indexPath.row] AndStrSize:11 AndLabelWith:Width - 80];
            }
            return 40;
            break;
            
        case 1://股东信息（自定义cell）
            return 112;
            break;
            
        case 2://高管信息
            return 40;
            break;
            
        case 3://自定义变更信息，根据变更信息数据，改变cell高度
            if(([CFToolUtil getLabelHeightWith:model.BeforeContent AndStrSize:11 AndLabelWith:Width - 40] + [CFToolUtil getLabelHeightWith:model.AfterContent AndStrSize:11 AndLabelWith:Width - 80]) < 60){
                return 152;
            }
            return 92 + [CFToolUtil getLabelHeightWith:model.BeforeContent AndStrSize:11 AndLabelWith:Width - 40] + [CFToolUtil getLabelHeightWith:model.AfterContent AndStrSize:11 AndLabelWith:Width - 80];
            break;
            
        default://分支信息
            return 40;
            break;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        //基本信息
        static NSString *iden = @"iden";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:iden];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        cell.textLabel.text = _localDataArray[indexPath.row];
        cell.textLabel.font = [UIFont systemFontOfSize:13.0];
        cell.detailTextLabel.text = _webDataArray[indexPath.row];
        cell.detailTextLabel.textColor = [UIColor colorWithHexString:@"#A1A1A1"];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:11.0];
        cell.detailTextLabel.numberOfLines = 0;
        
        return cell;
        
    }else if (indexPath.section == 1){
        //股东信息
        
        GSPartnerModel *model = _GSPartnerArray[indexPath.row];
        
        ShareholdCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShareholdCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.shareholdNameLabel.text = model.StockName;
        cell.rateLabel.text = model.StockPercent;
        cell.moneyLabel.text = model.ShouldCapi;
        cell.typeLabel.text = model.StockType;
        cell.dateLabel.text = [model.ShoudDate substringToIndex:10];
        
        return cell;
    }else if (indexPath.section == 2){
        //高管信息
        
        GSEmployeeModel *model = _GSEmployeeArray[indexPath.row];
        
        static NSString *identwo = @"identwo";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identwo];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identwo];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.textLabel.text = model.Name;
        cell.detailTextLabel.text = model.Job;
        cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
        
        return cell;
    }else if (indexPath.section == 3){
        //变更信息
        
        GSRecordModel *model = _GSRecordArray[indexPath.row];
        
        ChangeInforCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChangeInforCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.timeLabel.text = [model.ChangeDate substringToIndex:10];
        cell.myTitleLabel.text = model.ProjectName;
        cell.beforeLabel.text = model.BeforeContent;
        cell.afterLabel.text = model.AfterContent;
        cell.numberLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row + 1];
        
        return cell;
    }else{
        //分支机构
        
        GSBranchModel *model = _GSBranchArray[indexPath.row];
        
        static NSString *identhress = @"identhree";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identhress];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identhress];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.textLabel.text = model.Name;
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIButton *headView = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, Width, 40)];
    headView.backgroundColor = [UIColor whiteColor];
    [headView addTarget:self action:@selector(headClick:) forControlEvents:UIControlEventTouchUpInside];
    headView.tag = 2000 + section;
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, Width - 100, 40)];
    label.text = _headTitleArray[section];
    label.font = [UIFont boldSystemFontOfSize:14];
    label.textAlignment = NSTextAlignmentLeft;
    [headView addSubview:label];
    
    UILabel *noLabel = [[UILabel alloc]initWithFrame:CGRectMake(Width - 100, 0, 80, 40)];
    noLabel.text = @"无";
    noLabel.font = [UIFont systemFontOfSize:14];
    noLabel.textAlignment = NSTextAlignmentRight;
    noLabel.textColor = [UIColor colorWithHexString:@"#A1A1A1"];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(Width - 40, 8, 24, 24)];
    if([_closeArray[section] intValue]){
        imageView.image = UpImage;
    }else{
        imageView.image = DownImage;
    }

    
    UILabel *numLabel = [[UILabel alloc]initWithFrame:CGRectMake(Width - 80, 0, 40, 40)];
    numLabel.text = _headNumArray[section];
    numLabel.font = [UIFont systemFontOfSize:14];
    numLabel.textAlignment = NSTextAlignmentRight;
    numLabel.textColor = [UIColor colorWithHexString:@"#A1A1A1"];
    
    
    if((_GSPartnerArray.count == 0 && section == 1) || (_GSEmployeeArray.count == 0 && section == 2) || (_GSRecordArray.count == 0 && section == 3) || (_GSBranchArray.count == 0 && section == 4)){
        [headView addSubview:noLabel];
    }else{
        if(section > 0){
            [headView addSubview:numLabel];
        }
        [headView addSubview:imageView];
    }
    
    return headView;
}

- (void)headClick:(UIButton *)button{
    BOOL isClose = [[_closeArray objectAtIndex:button.tag - 2000] boolValue];
    [_closeArray replaceObjectAtIndex:button.tag - 2000 withObject:[NSNumber numberWithBool:!isClose]];
    
    //NSIndexSet:数字的集合
    //只刷新对应的段的数据
    [_tableView reloadSections:[NSIndexSet indexSetWithIndex:button.tag - 2000] withRowAnimation:UITableViewRowAnimationAutomatic];
}



- (void)loadData{
    NSMutableDictionary *para = [NSMutableDictionary dictionaryWithCapacity:0];
    [para setObject:Token forKey:@"key"];
    [para setObject:_keyNo forKey:@"keyword"];
    
    [CFHttpTools getRequest:@"ECIV4/GetDetailsByName" params:para success:^(id responseObj) {
        //
        CFLog(@"%@",responseObj);
        
        NSDictionary *resultDic = responseObj[@"Result"];
        
        _localDataArray = @[@"法定代表人",@"成立日期",@"注册资本",@"企业类型",@"登记状态",@"统一社会信用代码",@"工商注册号",@"组织机构代码",@"公司地址",@"营业开始时间",@"营业结束时间",@"核准日期",@"登记机关",@"经营范围"];
        _webDataArray = @[resultDic[@"OperName"],[resultDic[@"StartDate"] substringToIndex:10],resultDic[@"RegistCapi"],resultDic[@"EconKind"],resultDic[@"Status"],resultDic[@"CreditCode"],resultDic[@"No"],resultDic[@"OrgNo"],resultDic[@"Address"],[resultDic[@"TermStart"] substringToIndex:10],[resultDic[@"TeamEnd"] substringToIndex:10],[resultDic[@"CheckDate"] substringToIndex:10],resultDic[@"BelongOrg"],resultDic[@"Scope"]];
        
        //股东信息
        NSArray *partnerArray = resultDic[@"Partners"];
        
        JSONModelArray *partnerModelArray = [[JSONModelArray alloc]initWithArray:partnerArray modelClass:[GSPartnerModel class]];
        
        self.GSPartnerArray = [NSMutableArray array];
        
        [self.GSPartnerArray addObjectsFromArray:(NSArray *)partnerModelArray];
        
        //主要人员
        NSArray *employeeArray = resultDic[@"Employees"];
        
        JSONModelArray *employeeModelArray = [[JSONModelArray alloc]initWithArray:employeeArray modelClass:[GSEmployeeModel class]];
        
        self.GSEmployeeArray = [NSMutableArray array];
        
        [self.GSEmployeeArray addObjectsFromArray:(NSArray *)employeeModelArray];
        
        
        //变更历史
        NSArray *changeArray = resultDic[@"ChangeRecords"];
        
        JSONModelArray *changeModelArray = [[JSONModelArray alloc]initWithArray:changeArray modelClass:[GSRecordModel class]];
        
        self.GSRecordArray = [NSMutableArray array];
        
        [self.GSRecordArray addObjectsFromArray:(NSArray *)changeModelArray];
        
        
        NSArray *branchArray = resultDic[@"Branches"];
        
        JSONModelArray *branchModelArray = [[JSONModelArray alloc]initWithArray:branchArray modelClass:[GSBranchModel class]];
        
        self.GSBranchArray = [NSMutableArray array];
        
        [self.GSBranchArray addObjectsFromArray:(NSArray *)branchModelArray];
        
        _headNumArray = @[@"",[NSString stringWithFormat:@"%ld",_GSPartnerArray.count],[NSString stringWithFormat:@"%ld",_GSEmployeeArray.count],[NSString stringWithFormat:@"%ld",_GSRecordArray.count],[NSString stringWithFormat:@"%ld",_GSBranchArray.count]];
        
        _closeArray = [NSMutableArray arrayWithCapacity:0];
        for (NSInteger i = 0; i < 5; i ++) {
            [_closeArray addObject:[NSNumber numberWithBool:YES]];
        }
        
        [_tableView reloadData];
        
        
    } failure:^(NSError *error) {
        //
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
